package com.drinks.yiniang.ui.activity.goods;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.drinks.yiniang.R;
import com.drinks.yiniang.adapter.user.comments.GoodsCommentsAdapter;
import com.drinks.yiniang.common.recycler.decoration.FirstItemDecoration;
import com.drinks.yiniang.entity.comment.GoodsCommentJson;
import com.drinks.yiniang.entity.common.StringJson;
import com.drinks.yiniang.io.api.GoodsApiIO;
import com.drinks.yiniang.ui.widget.recycler.BaseRecyclerView;
import com.drinks.yiniang.utils.ToastUtils;
import com.hansen.library.Constants;
import com.hansen.library.io.tuple.Tuple2;
import com.hansen.library.listener.APIRequestCallback;
import com.hansen.library.listener.OnNavgationBarClickListener;
import com.hansen.library.ui.activity.BaseTranBarActivity;
import com.hansen.library.ui.widget.nav.NavigationBarLayout;
import com.hansen.library.utils.ColorUtils;
import com.hansen.library.utils.CommonUtils;
import com.hansen.library.utils.StringUtils;

import java.util.ArrayList;

/**
 * 全部评价
 *
 * @author han
 * @date 2019/09/10
 */
public class GoodsCommentsActivity extends BaseTranBarActivity implements OnNavgationBarClickListener,
        BaseQuickAdapter.RequestLoadMoreListener, BaseQuickAdapter.OnItemChildClickListener {

    //nav
    private NavigationBarLayout nav_bar_goods_comments;
    //全部评价
    private TextView tv_goods_comments_all;
    //专家点评
    private TextView tv_goods_comments_specialist;
    //评价列表
    private BaseRecyclerView rcv_goods_comments;

    private GoodsCommentsAdapter mCommentsAdapter;

    //商品id
    private String mGoodsId;
    //评论类型
    private String mCurrentType;
    private int mPages;

    @Override
    protected void initView() {
        nav_bar_goods_comments = findViewById(R.id.nav_bar_goods_comments);

        tv_goods_comments_all = findViewById(R.id.tv_goods_comments_all);
        tv_goods_comments_specialist = findViewById(R.id.tv_goods_comments_specialist);

        rcv_goods_comments = findViewById(R.id.rcv_goods_comments);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mContext = this;

        mGoodsId = getIntent() != null ? getIntent().getStringExtra(Constants.KeyGoodsId) : null;

        initAdapter();
        switchCommentsType(getIntent() != null ? getIntent().getStringExtra(Constants.KeyType) : Constants.TYPE_GOODS_COMMENTS_COMMON);
        getGoodsComment();
    }

    private void initAdapter() {
        rcv_goods_comments.setLayoutManager(new LinearLayoutManager(mContext));
        rcv_goods_comments.addItemDecoration(FirstItemDecoration.getItemDPDecoration(10));

        mCommentsAdapter = new GoodsCommentsAdapter(mContext, new ArrayList<GoodsCommentJson.GoodsCommentList>());
        mCommentsAdapter.setCenterEmpty(getLayoutInflater(), rcv_goods_comments);
        mCommentsAdapter.bindToRecyclerView(rcv_goods_comments);
    }

    @Override
    protected void initListener() {
        nav_bar_goods_comments.setOnNavgationBarClickListener(this);
        tv_goods_comments_all.setOnClickListener(this);
        tv_goods_comments_specialist.setOnClickListener(this);
        mCommentsAdapter.setOnItemChildClickListener(this);
        mCommentsAdapter.setOnLoadMoreListener(this, rcv_goods_comments);
    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.tv_goods_comments_all://全部评价
                switchCommentsType(Constants.TYPE_GOODS_COMMENTS_COMMON);
                getGoodsComment();
                break;
            case R.id.tv_goods_comments_specialist://专家点评
                switchCommentsType(Constants.TYPE_GOODS_COMMENTS_SPECIALIST);
                getGoodsComment();
                break;
            default:
                break;
        }
    }

    @Override
    public int getTranBarResID() {
        return R.layout.activity_goods_comments;
    }

    @Override
    public void onBackClick(View v) {
        finish();
    }

    @Override
    public void onEditClick(View v) {

    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        GoodsCommentJson.GoodsCommentList data = mCommentsAdapter.getItem(position);

        if (data == null) {
            ToastUtils.showShort(R.string.text_comment_not_exist);
            return;
        }

        if (view.getId() == R.id.tv_comments_like) {
            doCommentLikeOrUnLike(data.getId(), position);
        }
    }

    //评论点赞或取消点赞
    private void doCommentLikeOrUnLike(String commentId, final int position) {
        if (StringUtils.isEmpty(commentId)) {
            ToastUtils.showShort(R.string.text_comment_not_exist);
            return;
        }

        showLoadingDialog();

        GoodsApiIO.getInstance().doGoodsCommentsLike(commentId, new APIRequestCallback<StringJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(StringJson res) {
                GoodsCommentJson.GoodsCommentList data = mCommentsAdapter.getItem(position);

                if (data == null) {
                    return;
                }

                data.setIs_like(Constants.STATUS_GLOBAL_YES.equals(data.getIs_like()) ? Constants.STATUS_GLOBAL_NO : Constants.STATUS_GLOBAL_YES);
                data.setLike_count(res.getData());

                mCommentsAdapter.setData(position, data);
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
            }

            @Override
            public void onComplete() {
                dismissDialog();
            }
        });
    }

    @Override
    public void onLoadMoreRequested() {
        GoodsApiIO.getInstance().getGoodsComments(mGoodsId, mCurrentType, mPages,
                new APIRequestCallback<GoodsCommentJson, Tuple2<Integer, String>>() {
                    @Override
                    public void onSuccess(GoodsCommentJson res) {
                        if (mCommentsAdapter == null) {
                            return;
                        }

                        mPages++;
                        mCommentsAdapter.addData(res.getData().getData());

                        if (CommonUtils.isHasMoreData(res.getData().getData())) {
                            mCommentsAdapter.loadMoreComplete();
                        } else {
                            mCommentsAdapter.loadMoreEnd();
                        }
                    }

                    @Override
                    public void onError(Tuple2<Integer, String> error) {
                        ToastUtils.showShort(error._2);
                        mCommentsAdapter.loadMoreFail();
                    }

                    @Override
                    public void onComplete() {
                    }
                });
    }

    //获取评价
    private void getGoodsComment() {
        if (StringUtils.isEmpty(mGoodsId)) {
            ToastUtils.showShort(R.string.text_the_goods_does_not_exist);
            return;
        }

        showLoadingDialog();

        mPages = 1;

        mCommentsAdapter.getData().clear();
        mCommentsAdapter.notifyDataSetChanged();

        GoodsApiIO.getInstance().getGoodsComments(mGoodsId, mCurrentType, mPages,
                new APIRequestCallback<GoodsCommentJson, Tuple2<Integer, String>>() {
                    @Override
                    public void onSuccess(GoodsCommentJson res) {
                        if (mCommentsAdapter == null) {
                            return;
                        }

                        mPages++;
                        mCommentsAdapter.getData().clear();
                        mCommentsAdapter.addData(res.getData().getData());

                        if (CommonUtils.isHasMoreData(res.getData().getData())) {
                            mCommentsAdapter.loadMoreComplete();
                        } else {
                            mCommentsAdapter.loadMoreEnd();
                        }
                    }

                    @Override
                    public void onError(Tuple2<Integer, String> error) {
                        ToastUtils.showShort(error._2);
                        mCommentsAdapter.loadMoreFail();
                    }

                    @Override
                    public void onComplete() {
                        dismissDialog();
                    }
                });
    }

    private void switchCommentsType(@NonNull String type) {
        if(mCurrentType == null) {
            mCurrentType = Constants.TYPE_GOODS_COMMENTS_COMMON;
        }

        if (mCurrentType.equals(type)) {
            return;
        }

        mCurrentType = type;

        if (Constants.TYPE_GOODS_COMMENTS_SPECIALIST.equals(type)) {
            tv_goods_comments_all.setTextColor(ColorUtils.getColorById(mContext, R.color.color_282829));
            tv_goods_comments_specialist.setTextColor(ColorUtils.getColorById(mContext, R.color.color_ef3024));
        } else {
            tv_goods_comments_all.setTextColor(ColorUtils.getColorById(mContext, R.color.color_ef3024));
            tv_goods_comments_specialist.setTextColor(ColorUtils.getColorById(mContext, R.color.color_282829));
        }
    }
}
